/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.engine;

import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author Rainbow
 */
@Component("CebpubserviceSpider")
@Scope("prototype")
public class CebpubserviceSpider extends RollPageSpider
{
    @Override
    public SgmlPage openPage(String url, int multiple) throws Exception
    {
        SgmlPage page = null;
        int i = 1;
        while (i <= 5)
        {
            try
            {
                WebRequest wr = new WebRequest(new URL(url));
                wr.setAdditionalHeader("Referer", mySite.siteUrl);
                wr.setRequestParameters(generateRequestParameters());
                page = (SgmlPage) webClient.getPage(wr);
                if (mySite.jsEnabled)
                {
                    webClient.waitForBackgroundJavaScript(multiple * 5000L);
                }
                break;
            }
            catch (Exception e)
            {
                TimeUnit.SECONDS.sleep(2);
                logger.warn("open ({}) failed, try again ... ({}), ",url, e.getMessage());
            }
            finally
            {
                ++i;
            }
        }
        return page;
    }
    
    private List<NameValuePair> generateRequestParameters()
    {
        List<NameValuePair> body = new ArrayList<>();
        NameValuePair pair = new NameValuePair("searchName", "综合监控");
        body.add(pair);
        pair = new NameValuePair("businessType", "招标公告");
        body.add(pair);
        pair = new NameValuePair("bulletinIssnTime", "有效期内");
        body.add(pair);
        pair = new NameValuePair("pageNo","1");
        body.add(pair);
        pair = new NameValuePair("row", "50");
        body.add(pair);
        return body;
    }
}
